package com.hzncc.flowable_diboot.flowable.servicetask.refund;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diboot.core.holder.ThreadLocalHolder;
import com.diboot.core.util.ContextHolder;
import com.hzncc.flowable_diboot.contract.service.ContractAuditService;
import com.hzncc.flowable_diboot.contract.service.ContractInfoService;
import com.hzncc.flowable_diboot.contract.service.impl.ContractAuditServiceImpl;
import com.hzncc.flowable_diboot.contract.service.impl.ContractInfoServiceImpl;
import com.hzncc.flowable_diboot.entity.SapCustomer;
import com.hzncc.flowable_diboot.entity.refundmanage.RefundContract;
import com.hzncc.flowable_diboot.entity.refundmanage.RefundRegister;
import com.hzncc.flowable_diboot.enums.BpmProcessInstanceStatusEnum;
import com.hzncc.flowable_diboot.payment.entity.OldReceive;
import com.hzncc.flowable_diboot.payment.service.OldReceiveService;
import com.hzncc.flowable_diboot.payment.service.impl.OldReceiveServiceImpl;
import com.hzncc.flowable_diboot.service.SapCustomerService;
import com.hzncc.flowable_diboot.service.impl.SapCustomerServiceImpl;
import com.hzncc.flowable_diboot.service.refundmanage.RefundContractService;
import com.hzncc.flowable_diboot.service.refundmanage.RefundRegisterService;
import com.hzncc.flowable_diboot.service.refundmanage.impl.RefundContractServiceImpl;
import com.hzncc.flowable_diboot.service.refundmanage.impl.RefundRegisterServiceImpl;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class RevokeRefundRegisterAmount implements JavaDelegate {

    @Override
    @Transactional
    public void execute(DelegateExecution execution) {

        RefundContractService refundContractService = ContextHolder.getBean(RefundContractServiceImpl.class);
        RefundRegisterService refundRegisterService = ContextHolder.getBean(RefundRegisterServiceImpl.class);
        SapCustomerService sapCustomerService = ContextHolder.getBean(SapCustomerServiceImpl.class);
        OldReceiveService oldReceiveService = ContextHolder.getBean(OldReceiveServiceImpl.class);

        //当前正在执行任务对应的流程实例
        String instanceId = execution.getProcessInstanceId();
        LambdaQueryWrapper<RefundContract> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RefundContract::getInstanceId,instanceId).eq(RefundContract::isDeleted,0);
        ThreadLocalHolder.setIgnoreInterceptor();
        RefundContract refundContract = refundContractService.getSingleEntity(queryWrapper);
        refundContract.setProcessState(String.valueOf(BpmProcessInstanceStatusEnum.REJECT.getStatus()));
        ThreadLocalHolder.setIgnoreInterceptor();
        refundContractService.updateEntity(refundContract);
        ThreadLocalHolder.setIgnoreInterceptor();
        RefundRegister refundRegister = refundRegisterService.getEntity(refundContract.getRefundRegisterId());

        //更新退款登记金额
        BigDecimal refundAmount = refundRegister.getRefundAmount();
        BigDecimal returnAmount = refundRegister.getReturnAmount();//已退款金额
        BigDecimal noReturnAmount;//未退款金额
        BigDecimal currentRefundAmount = refundContract.getCurrentRefundAmount();
        if (ObjectUtils.isNotEmpty(refundAmount)&&
                ObjectUtils.isNotEmpty(currentRefundAmount)){
            returnAmount = returnAmount.subtract(currentRefundAmount);
            noReturnAmount = refundAmount.subtract(returnAmount);
            LambdaUpdateWrapper<RefundRegister> refundRegisterUpdate = new LambdaUpdateWrapper<>();
            refundRegisterUpdate.set(RefundRegister::getReturnAmount,returnAmount)
                    .set(RefundRegister::getNoReturnAmount,noReturnAmount)
                    .eq(RefundRegister::getId,refundContract.getRefundRegisterId());
            ThreadLocalHolder.setIgnoreInterceptor();
            refundRegisterService.updateEntity(refundRegisterUpdate);
        }
        //如果是老应收，更新老应收回款金额
        if ("20".equals(refundContract.getRefundContractCategory())){
            ThreadLocalHolder.setIgnoreInterceptor();
            SapCustomer sapCustomer = sapCustomerService.getEntity(refundContract.getSapCustomerId());
            String sapNo = sapCustomer.getSapNo();
            String companyCode = sapCustomer.getCompanyCode();
            LambdaQueryWrapper<OldReceive> oldReceiveQuery = new LambdaQueryWrapper<>();
            oldReceiveQuery.eq(OldReceive::getCompanyCode,companyCode).eq(OldReceive::getSapNo,sapNo).eq(OldReceive::isDeleted,0);
            ThreadLocalHolder.setIgnoreInterceptor();
            OldReceive oldReceive = oldReceiveService.getSingleEntity(oldReceiveQuery);
            if (ObjectUtils.isNotEmpty(currentRefundAmount)&&ObjectUtils.isNotEmpty(refundContract.getDirectRefundAmount())){
                oldReceive.setReceivedAmount(oldReceive.getReceivedAmount().add(currentRefundAmount.subtract(refundContract.getDirectRefundAmount())));
            }else {
                oldReceive.setReceivedAmount(oldReceive.getReceivedAmount());
            }
            oldReceive.setUnreceivedAmount(oldReceive.getReceivableAmount().subtract(oldReceive.getReceivedAmount()));
            ThreadLocalHolder.setIgnoreInterceptor();
            oldReceiveService.updateEntity(oldReceive);
        }
    }
}
